

import UIKit

class XWDataStateTool: NSObject {
    static let shareManger = XWDataStateTool.init()
    private var isConnectVPN:Bool = false
    private var nodeInfoModel:XWNodeInfoModel!
    private var timer:Timer?
    private var timerNumber:Int = 0
    private var selectedIndexRow:Int = 0
    // 计时器
    var timerLabel:UILabel?
    // 下行
    var XWDownloadLabl: UILabel?
    // 上行
    var XWUploadLabel: UILabel?
    
    var XWSpeedLabel:UILabel?
    func getIsConnectVPN() -> Bool{
        return isConnectVPN
    }
    func setIsConnectVPN(_ newValue: Bool){
        isConnectVPN = newValue
    }
    func getSelectedIndexRow() -> Int {
        return selectedIndexRow
    }
    func setSelectedIndexRow(_ newIndexRow:Int){
        selectedIndexRow = newIndexRow
    }
    func getXWNodeInfoModel() -> XWNodeInfoModel {
        return nodeInfoModel
    }
    func setXWNodeInfoModel(_ model:XWNodeInfoModel) {
        nodeInfoModel = model
    }
    func startTimer(){
        invalidateTimer()
        timer = Timer.scheduledTimer(timeInterval: 1,target:self,selector:#selector(timerFire),userInfo:nil,repeats:true)
    }
    func invalidateTimer() {
        timer?.invalidate()
    }
    func resetTime(){
        timerNumber = 0
    }
    @objc private func timerFire() {
        if isConnectVPN {
            timerNumber += 1
            let hh = timerNumber / 3600
            let mm = timerNumber / 60
            let ss = timerNumber % 60
            if timerNumber % 2 == 0{
                XWDownloadLabl?.text =  String(Double(arc4random()%3) + 0.1) + "mb/s"
                XWUploadLabel?.text = String(Double(arc4random()%3) + 0.1) + "mb/s"
                XWSpeedLabel?.text = String(Double(arc4random()%3) + 0.1)
            }
            var tempHH = "00"
            var tempMM = "00"
            var tempSS = "00"
            if hh<10 {
                tempHH = String("0\(hh)")
            }else{
                tempHH = String(hh)
            }
            if mm < 10 {
                tempMM = String("0\(mm)")
            }else{
                tempMM = String(mm)
            }
            if ss < 10{
                tempSS = String("0\(ss)")
            }else{
                tempSS = String(ss)
            }
            timerLabel?.text = String("\(tempHH):\(tempMM):\(tempSS)")
        }else{
            XWDownloadLabl?.text = "0.00 mb/s"
            XWUploadLabel?.text = "0.00 mb/s"
            timerLabel?.text = "00:00:00"
        }
    }
    
    
}

